// pages/index3/index3.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pay_gold:false,
    input:"请输入内容",
    Hidden: false,
    fileHidden: true,
    gold_input:0,

    //问题id
    qes_id: null, //从跳转页面onload函数获得
    qesUser_id: null, //提问者user_id
    qes_content: "", //提问内容

    //文件信息
    type: "", //类型
    author: "", //作者
    remark: "", //备注
    gold_num: 0, //文件下载所需金币数
    file_url: "", //临时文件路径
    file_name: "", //文件名
    file_time: 0, //文件上传时间
    file_size: "", //文件大小
    file_suffix: "", //文件后缀

    //评论内容
    comment_cont: "",

  },

  //鼠标聚焦
  focus:function(){
    this.setData({
      gold_input:"",
      input:"",
    })
  },

  //鼠标移开
  blur:function(){
    this.setData({
      gold_input:0,
      input:"请输入内容"
    })
  },

  

  onLoad: function (e) {
    console.log(e)
    this.setData({
      qes_id: e.qes_id,
      qesUser_id: e.qesUser_id,
      qes_content: e.qes_content,
    })
  },

  //隐藏和显示信息
  click: function (e) {
    this.setData({
      Hidden: !this.data.Hidden,
    })
  },

  //获取单选框的值
  getRadio(e) {
    console.log('单选框的值：', e.detail.value)
    this.setData({
      type: e.detail.value
    })
  },
  //获取作者
  getAuthor(e) {
    console.log('备注输入框的值：', e.detail.value)
    this.setData({
      author: e.detail.value,
    })
  },
  //获取备注
  getRemark(e) {
    console.log('备注输入框的值：', e.detail.value)
    this.setData({
      remark: e.detail.value,
    })
  },
  //获取金币
  getGoldNum(e) {
    console.log('金币输入框的值99：', e.detail.value)
    this.setData({
      gold_num: e.detail.value,
    })
  },

  //获取评论内容
  getComment(e) {
    console.log('评论输入框的值：', e.detail.value)
    this.setData({
      comment_cont: e.detail.value,
    })
  },

  //提交评论
  onComment() {
    var that = this
    var userInfo = getApp().globalData.userInfo //获取全局的用户信息

    if (that.data.comment_cont == "") {
      wx.showModal({
        title: '温馨提示',
        content: '评论内容不能为空',
        showCancel: false,
      })
    } else if(userInfo.user_id==undefined){
         //跳转至授权登录界面
         wx.showModal({
          title: '提示!',
          content: '请授权后再使用小程序',
          showCancel: false,
          success(res) { 
            wx.navigateTo({
              url: '../login/login',
            })
          }
        })
    }else{
      //获取当前时间戳
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      console.log(timestamp)
      wx.request({
        url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/comment_answer',
        method: "POST", //请求方式
        header: {
          'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
        },
        data: {
          qes_id: that.data.qes_id, //问题id
          qesUser_id: that.data.qesUser_id,
          qes_content: that.data.qes_content,

          //用户信息
          user_id: userInfo.user_id, //不能为空，否则用户需先授权登录
          user_school: userInfo.user_school,
          user_faculty: userInfo.user_faculty,
          user_name: userInfo.user_name,
          user_picurl: userInfo.user_picurl, //用户头像路径

          content: that.data.comment_cont, //评论内容

          time: timestamp //评论时间
        },
        success(res) {
          console.log(res.data)
          if (res.data.error_code == 1) { //用户未登录
            //跳转至授权登录界面
          wx.showModal({
            title: '提示!',
            content: '请授权后再使用小程序',
            showCancel: false,
            success(res) { 
              wx.navigateTo({
                url: '../login/login',
              })
            }
          })
           
          } else if (res.data.error_code == 0) { //评论成功
            wx.showToast({
              title: '评论成功',
              icon: 'success',
              success() {
                //交互完毕
                //输入框清空

                //评论框清空
                that.setData({
                  comment_cont: "",
                })
              },
              complete: function (res) {   //要在complete跳转，否则没等用户点击确定就跳转了，体验不好
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1,
                  })
                }, 1000);
              }
            })
          }
        }
      })
    }

  },

  //选择文件
  selectFile: function (e) {
    var that = this

    wx.chooseMessageFile({ //从聊天记录中选择文件
      count: 1, //文件数量
      type: 'file', //可以选择除了图片和视频之外的其它的文件
      success(res) {
        wx.showLoading({
          title: '加载中...',
        })
        console.log('选择文件的返回值：', res)

        const tempFilePaths = res.tempFiles[0].path //临时文件路径

        //获取文件后缀
        const houzhui = tempFilePaths.match(/\.[^.]+?$/)[0]; //文件后缀，不知道这串代码什么意思
        console.log(houzhui)

        //获取当前时间戳
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        console.log(timestamp)

        //获取文件大小
        var fileSize = res.tempFiles[0].size / 1024;
        console.log(fileSize)
        if (fileSize / 1024 > 100) { //文件大于100M
          wx.showModal({
            title: '温馨提示',
            content: '文件不能大于100M！',
            showCancel: false,
          })
        } else {
          if ((fileSize / 1024) > 1) {
            fileSize = (fileSize / 1024).toFixed(1) + ' M'; //保留小数点一位
          } else {
            fileSize = fileSize.toFixed(1) + ' K';
          }
          console.log(fileSize)

          that.setData({
            file_name: res.tempFiles[0].name,
            file_size: fileSize,
            file_time: timestamp,
            file_suffix: houzhui,
            file_url: tempFilePaths,
            fileHidden: false
          })

        

        }

      },complete:function(){
        wx.hideLoading()
      }
    })
  },

  //提交信息
  onSubmit: function (e) {
    var that = this
    var userInfo = getApp().globalData.userInfo
    that.setData({
      pay_gold:true
    })
   if(userInfo.user_id==undefined){
       //跳转至授权登录界面
       wx.showModal({
        title: '提示!',
        content: '请授权后再使用小程序',
        showCancel: false,
        success(res) { 
          wx.navigateTo({
            url: '../login/login',
          })
        }
      })
   }else{
    wx.showLoading({
      title: '上传中...',
    })
       // 文件上传
    wx.uploadFile({
      filePath: that.data.file_url,
      name: 'file',
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/file_answer',
      method: "POST", //请求方式
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      formData: {
        qes_id: that.data.qes_id, //问题id
        qesUser_id: that.data.qesUser_id,
        qes_content: that.data.qes_content,

        //用户信息
        user_id: userInfo.user_id, //不能为空，否则用户需先授权登录
        user_school: userInfo.user_school,
        user_faculty: userInfo.user_faculty,
        user_name: userInfo.user_name,
        user_picurl: userInfo.user_picurl, //用户头像路径

        //文件信息
        type: that.data.type, //类型
        author: that.data.author, //作者
        remark: that.data.remark, //备注
        gold_num: that.data.gold_num, //文件下载所需金币数
        file_url: that.data.file_url, //临时文件路径，不能为空，否则用户未上传文件
        file_name: that.data.file_name, //文件名
        file_time: that.data.file_time, //文件上传时间
        file_size: that.data.file_size, //文件大小
        file_suffix: that.data.file_suffix, //文件后缀
      },
      fail() {
        wx.showModal({
          title: '温馨提示',
          content: '您还未上传文件哦~',
          showCancel: false
        })
      },
      success: function (res) {
        console.log('文件上传返回值:', res)
        var json = JSON.parse(res.data) // 返回值为string转换成json格式
        console.log(json)
        if (json.error_code == 1) { //用户未登录
          //跳转至授权登录界面
          wx.navigateTo({
            url: '../login/login',
          })
        } else if (json.error_code == 2) { //用户未上传文件
          wx.hideLoading({
            success: (res) => {
              wx.showModal({
                title: '温馨提示',
                content: '很抱歉，上传失败！',
                showCancel: false
              })
            },
          })
        } else if (json.error_code == 3) { //上传失败
          wx.hideLoading({
            success: (res) => {
              wx.showModal({
                title: '温馨提示',
                content: '很抱歉，上传失败！',
                showCancel: false
              })
            },
          })
        } else if (json.error_code == 0) { //上传成功
          wx.showToast({
            title: '上传成功',
            icon: 'success',
            success() {
              //交互完毕
              //输入框清空
              //文件上传框清空
              that.setData({
                pay_gold:false,
                file_url: "",
                author: "",
                remark: "",
                gold_num: 0,
                fileHidden: true,
              })
            },
            complete: function (res) {   //要在complete跳转，否则没等用户点击确定就跳转了，体验不好
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                })
              }, 1000);
            }
          })
        }
      }
    })
   }
   
  },
})